<?php

class Painel_Form_Usuarios extends Zend_Form
{

    public function init()
    {
        $front = Zend_Controller_Front::getInstance();
        $controller = $front->getRequest()->getControllerName();
        $module = $front->getRequest()->getModuleName();
        $url = $front->getBaseUrl().'/'.$module.'/'.$controller;
        
        $this->setName('form_edt');
        
        $db = Zend_Db_Table_Abstract::getDefaultAdapter();
        
        $auth = Zend_Auth::getInstance();
        $s_user = $auth->getStorage();
        $_PER_ID = $s_user->read()->PER_ID;
        $_USU_ID = $s_user->read()->USU_ID;
        
        $el = new Zend_Form_Element_Hidden('USU_ID',array('disableLoadDefaultDecorators' =>true));
        $el->addDecorators(array(array('ViewHelper'),array('Errors')))
            ->addFilter('Int');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('USU_NOM');
        $el->setLabel('Nome')
        ->setRequired(true)
        ->addFilter('StripTags')
        ->addFilter('StringTrim')
        ->addValidator('NotEmpty');
        $el->setAttrib('class', 'form-control');
        $el->setAttrib('required', 'required');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Text('USU_EML');
        $el->setLabel('E-mail')
        ->setRequired(true)
        ->addFilter('StripTags')
        ->addFilter('StringTrim')
        ->addValidator('NotEmpty');
        $el->setAttrib('class', 'form-control email'); 
        $el->setAttrib('required', 'required');
        $this->addElement($el);
        
        $el= new Zend_Form_Element_Text('USU_LOG');
        $el->setLabel('Login')
        ->setRequired(true)
        ->addFilter('StripTags')
        ->addFilter('StringTrim')
        ->addValidator('NotEmpty');
        $el->setAttrib('class', 'form-control');
        $el->setAttrib('required', 'required');
        $this->addElement($el);
        
        $el = new Zend_Form_Element_Password('USU_SNH');
        $el->setLabel('Senha')
        //->setRequired(true)
        ->addFilter('StripTags')
        ->addFilter('StringTrim')
        ->addValidator('NotEmpty');
        $el->setAttrib('class', 'form-control');
        $this->addElement($el);
        
        if($_PER_ID <= 5){
            $obj = new Painel_Model_DbTable_Permissao();
            $row = $obj->fetchAll()->toArray();
            $arr = array();
            foreach($row as $r) $arr[$r['PER_ID']] = $r['PER_NOM']; 

            // <> de Web mastar
            if($_PER_ID > 1) unset($arr['1']);

            $el = new Zend_Form_Element_Select('PER_ID',array('onchange' => ''));
            $el->setLabel('Permissão')
            ->setRequired(true)
            ->addFilter('StripTags')
            ->addFilter('StringTrim')
            ->addValidator('NotEmpty')
            ->addMultiOptions($arr);
            $el->setAttrib('class', 'form-control');
            $this->addElement($el);
        
        
            $query = 'SELECT SQL_CACHE TUS_ID,TUS_NOM FROM TIPO_USUARIO WHERE TUS_STA = 1 ORDER BY TUS_NOM ASC';
            $row = $db->fetchAll($query);
            $arr = array();
            foreach($row as $r) $arr[$r['TUS_ID']] = $r['TUS_NOM']; 

            $el = new Zend_Form_Element_Select('TUS_ID',array('onchange' => ''));
            $el->setLabel('Tipo de usuário')
            ->addMultiOptions($arr);
            $el->setAttrib('class', 'form-control required');
            $this->addElement($el);
        
        
            $el = new Zend_Form_Element_Checkbox('USU_STA',array('checked'=>'checked'));
            $el->setLabel('Status');
            $this->addElement($el);
        }
        
        $el = new Zend_Form_Element('br',array( 'helper'=>'formNote', 'value'=>'&nbsp;' ));
        $this->addElement($el);
        
        $btn = new Zend_Form_Element_Button('Salvar',array('disableLoadDefaultDecorators' =>true));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<button type="submit" class="btn btn-lg btn-success"><span class="glyphicon glyphicon-floppy-saved" aria-hidden="true"></span> Salvar</button>');
        $this->addElement($btn);
        
        $btn = new Zend_Form_Element_Button('Cancelar',array('disableLoadDefaultDecorators' =>true));
        $btn->setDecorators(array(array('Description', array('escape' => false, 'tag' => false))));
        $btn->setDescription('<a href="'.$url.'" class="btn btn-lg btn-danger"><span class="glyphicon glyphicon-floppy-remove" aria-hidden="true"></span> Cancelar</a>');
        $this->addElement($btn);
        
    }


}

